<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * Authen Service
 *
 */
class SDOServiceAuthen extends ServiceBase
{
    # 正式环境添加应用接口
    const AUTHEN_APP_OP_URL = 'AUTHEN_APP_OP_URL';
    # 测试环境添加应用接口
    const PRE_AUTHEN_APP_OP_URL = 'PRE_AUTHEN_APP_OP_URL';
    # 正式环境查询应用接口
    const AUTHEN_APP_SEARCH_URL = 'AUTHEN_APP_SEARCH_URL';
    # 测试环境查询应用接口
    const PRE_AUTHEN_APP_SEARCH_URL = 'PRE_AUTHEN_APP_SEARCH_URL';
    # 正式环境信任IP接口
    const IP_AUTHEN_TRUST_OP_URL = 'IP_AUTHEN_TRUST_OP_URL';
    # 测试环境信任IP接口
    const PRE_AUTHEN_IP_TRUST_OP_URL = 'PRE_AUTHEN_IP_TRUST_OP_URL';
    # 玩家游戏内测属性修改接口
    const MODIFY_PERM_USER = "MODIFY_PERM_USER";
    const PRE_MODIFY_PERM_USER = "MODIFY_PERM_USER";
    # 玩家游戏内测属性查询接口
    const QUERY_PERM_USER = "QUERY_PERM_USER";
    const PRE_QUERY_PERM_USER = "QUERY_PERM_USER";
    const CAS_WSDL = "AUTHEN_CAS_WSDL";
    const PRE_CAS_WSDL = "AUTHEN_CAS_WSDL";
    const APP_TEST_STATUS = 1;
    const APP_ONLINE_STATUS = 2;
    protected static $testStatus = array(
        self::APP_TEST_STATUS => 1, 
        self::APP_ONLINE_STATUS => 0
    );
    /**
     * 开通认证、添加应用
     *
     * @param array|string $message // array(code,msg)
     * @param int $appId
     * @param string $appName
     * @param int $test 应用状态 1测试 2正式
     * @param string $env
     * @return boolean
     */
    public static function addApp (&$message, $appId, $appName, $test = 1, $env = 'pre')
    {
        $op = 'add';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_AUTHEN_APP_OP_URL) : sdk_fetch_config(
        SDK_ENV . self::AUTHEN_APP_OP_URL);
        $url .= $op;
        $param['appId'] = $appId;
        $param['appName'] = iconv('utf-8', 'gb2312', $appName);
        $param['test'] = self::$testStatus[$test];
        $urlSuffix = strtr(http_build_query($param), array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '&' . $urlSuffix, $message);
        if ($content === FALSE) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\texception: " . $message, 
            __CLASS__);
            return FALSE;
        }
        if (! ($content === "0" || $content == - 610502 || $content == - 610302)) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
            __CLASS__);
            $message = $content;
            return false;
        }
        self::log("method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
        __CLASS__, true);
        return true;
    }
    /**
     * 查询应用信息
     *
     * @param array|string $message // array(code,msg)
     * @param int $appId
     * @param string $env
     * @return array|boolean
     */
    public static function searchApp (&$message, $appId, $env = 'pre')
    {
        $op = 'search';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_AUTHEN_APP_SEARCH_URL) : sdk_fetch_config(
        SDK_ENV . self::AUTHEN_APP_SEARCH_URL);
        $url .= $op;
        $param['appId'] = $appId;
        $urlSuffix = strtr(http_build_query($param), array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '&' . $urlSuffix, $message);
        if ($content === FALSE) {
            //$message = array('-2' , $message);
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != - 610601) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
            __CLASS__);
            $message = isset($result[1]) ? $result[1] : '接口错误' . $result[0];
            return false;
        }
        return $result;
    }
    /**
     * 修改应用编码接口的test属性值
     * 
     * @param string $message 错误信息
     * @param int $appId 应用ID
     * @param int $test 应用状态 1测试 2正式
     * @param string $env 环境
     * 
     * @return boolean
     */
    public static function editAppStatus (&$message, $appId, $test = 2, $env = 'pre')
    {
        $op = 'editTest';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_AUTHEN_APP_SEARCH_URL) : sdk_fetch_config(
        SDK_ENV . self::AUTHEN_APP_SEARCH_URL);
        $url .= $op;
        $param['appId'] = $appId;
        $param['test'] = self::$testStatus[$test];
        //$param['test'] = $test;
        $urlSuffix = strtr(http_build_query($param), 
        array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '&' . $urlSuffix, $message);
        if ($content === FALSE) {
            //$message = array('-2' , $message);
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\texception: " . $message, 
            __CLASS__);
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != - 611101) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
            __CLASS__);
            $message = isset($result[1]) ? $result[1] : '接口错误' . $result[0];
            return false;
        }
        self::log("method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
        __CLASS__, true);
        return true;
    }
    /**
     * 添加信任IP
     *
     * @param array|string $message // array(code,msg)
     * @param string $ip
     * @param string $env
     * @return boolean
     */
    public static function addTrustIP (&$message, $ip, $env = 'pre')
    {
        $op = 'add';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_AUTHEN_IP_TRUST_OP_URL) : sdk_fetch_config(
        SDK_ENV . self::IP_AUTHEN_TRUST_OP_URL);
        $url .= $op;
        $param['IP'] = $ip;
        $urlSuffix = strtr(http_build_query($param), array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '&' . $urlSuffix, $message);
        if ($content === FALSE) {
            //$message = array('-2' , $message);
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\texception: " . $message, 
            __CLASS__);
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != - 610701) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
            __CLASS__);
            $message = isset($result[1]) ? $result[1] : '接口错误' . $result[0];
            return false;
        }
        self::log("method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
        __CLASS__, true);
        return true;
    }
    /**
     * 查询信任IP
     *
     * @param array|string $message // array(code,msg)
     * @param string $ip
     * @param string $env
     * @return array|boolean
     */
    public static function searchTrustIP (&$message, $ip, $env = 'pre')
    {
        $op = 'search';
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_AUTHEN_IP_TRUST_OP_URL) : sdk_fetch_config(
        SDK_ENV . self::IP_AUTHEN_TRUST_OP_URL);
        $url .= $op;
        $param['IP'] = $ip;
        $urlSuffix = strtr(http_build_query($param), array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '&' . $urlSuffix, $message);
        if ($content === FALSE) {
            return FALSE;
        }
        $result = explode('^$^', $content);
        if ($result[0] != - 610801) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
            __CLASS__);
            $message = isset($result[1]) ? $result[1] : '接口错误' . $result[0];
            return false;
        }
        return true;
        //return in_array($result[0], array(- 610801 , - 610802)) ? $result : FALSE;
    }
    /**
     * 玩家游戏内测属性查询接口
     * @param mixed $message
     * @param string $uuid 数字账号
     * @param int $appId 游戏ID
     * @param int $gamearea 游戏区号
     * @param string $env 环境
     * 
     * @return boolean
     */
    public static function QueryPermissionUserGame (&$message, $uuid, $appId, $gamearea = 999, $env = 'pre')
    {
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_QUERY_PERM_USER) : sdk_fetch_config(
        SDK_ENV . self::QUERY_PERM_USER);
        $params = array(
            'uuid' => $uuid, 
            'appId' => $appId, 
            'appArea' => $gamearea
        );
        $urlSuffix = strtr(http_build_query($params), array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '?' . $urlSuffix, $message);
        if ($content === FALSE) {
            //$message = array('-2' , $message);            
            return FALSE;
        }
        $errMsg = array(
            1 => "权限受限", 
            2 => "系统错误，失败", 
            3 => "拒绝查询", 
            4 => "用户不存在", 
            5 => "账号无内测权限", 
            6 => "账号输入为空"
        );
        if ($content !== "0") {
            if ($content) {
                $message = isset($errMsg[$content]) ? $errMsg[$content] : "UNDEFINED ERROR:\t{$content}";
            }
            return false;
        }
        return true;
    }
    /**
     * 批量查询权限接口
     * @param array $uuids
     * @param int $appId
     * @param int $gamearea
     * @param string $env
     * 
     * return array($uid1=>false, $uid2=>true, ,,,);
     */
    public static function queryPermissionMulti ($uuids, $appId, $gamearea = 999, $env = 'pre')
    {
        $result = array();
        $message = '';
        foreach ($uuids as $uuid) {
            $result[$uuid] = self::QueryPermissionUserGame($message, $uuid, $appId, $gamearea, $env);
        }
        return $result;
    }
    /**
     * 玩家游戏内测属性批量修改接口
     * @param string $message
     * @param array $uuids
     * @param int $appId
     * @param int $gamearea 
     * @param int $permissionvalue 0 没有内测权限 1 有
     * @param string $env
     * 
     * return array(数字ID=>array(result=>true, message=>''), 数字ID2=>array(result=>true, message=>''),,)
     */
    public static function modifyPermissionMulti ($uuids, $appId, $gamearea = 999, $permissionvalue, $env = 'pre')
    {
        $result = array();
        foreach ($uuids as $uuid) {
            $row = array();
            $row['result'] = self::ModifyPermissionUserGame($row['message'], $uuid, $appId, $gamearea, 
            $permissionvalue, $env);
            $result[$uuid] = $row;
        }
        return $result;
    }
    /**
     * 玩家游戏内测属性修改接口
     * @param mixed $message
     * @param string $uuid 数字账号
     * @param int $appId 游戏ID
     * @param int $gamearea 游戏区号
     * @param int $permissionvalue 0 没有内测权限 1 有
     * @param string $env 环境
     * 
     * @return boolean
     */
    public static function ModifyPermissionUserGame (&$message, $uuid, $appId, $gamearea = 999, $permissionvalue, $env = 'pre')
    {
        $url = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_MODIFY_PERM_USER) : sdk_fetch_config(
        SDK_ENV . self::MODIFY_PERM_USER);
        $params = array(
            'uuid' => $uuid, 
            'appId' => $appId, 
            'appArea' => $gamearea, 
            'permissionvalue' => $permissionvalue
        );
        $urlSuffix = strtr(http_build_query($params), array(
            "&amp;" => "&"
        ));
        $content = self::getRestfulContent($url . '?' . $urlSuffix, $message);
        $errMsg = array(
            1 => "权限受限", 
            2 => "系统错误，失败", 
            3 => "拒绝查询", 
            4 => "用户不存在", 
            5 => "账号输入为空", 
            6 => "内测属性值为空或不合法"
        );
        if ($content === FALSE) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\texception: " . $message, 
            __CLASS__);
            return FALSE;
        }
        if ($content !== "0") {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
            __CLASS__);
            if ($content) {
                $message = isset($errMsg[$content]) ? $errMsg[$content] : "UNDEFINED ERROR:\t{$content}";
            }
            return false;
        }
        self::log("method: " . __FUNCTION__ . "\tparams: " . $urlSuffix . "\tenv: {$env}\treturn: " . $content, 
        __CLASS__, true);
        return true;
    }
    /**
     * 开通CAS白名单
     * @param string $message
     * @param int $appId
     * @param string $domain
     * @param boolean $isSelf 是否开启自身防沉迷
     * @param string $env
     */
    public function setCasDomain (&$message, $appId, $domain, $isSelf = false, $env = 'pre')
    {
        $wsdl = ($env === 'pre') ? sdk_fetch_config(SDK_ENV . self::PRE_CAS_WSDL) : sdk_fetch_config(
        SDK_ENV . self::CAS_WSDL);
        $params = array(
            'strAppID' => $appId, 
            'strDomainName' => $domain, 
            'iRetOid' => 0, 
            'iRetPt' => 0, 
            'iRetSDID' => 0, 
            'iRetSubApp' => 1, 
            'iEkeyType' => 0, 
            'iEkeyStatus' => 0, 
            'iIDCard' => 1, 
            'iValidate' => 1
        );
        if (! $isSelf) {
            $params['iIDCard'] = 0;
        }
        $options = array(
            'soap_version' => SOAP_1_2
        );
        $result = self::soapRequest('Set', $params, $wsdl, $options, $message);
        if ($result === FALSE) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $appId . "\tenv: {$env}\texception: " . $message, 
            __CLASS__);
            return FALSE;
        }
        if (! $result->service->recordCount) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . $appId . "\tenv: {$env}\treturn: " . json_encode(
            $result), __CLASS__);
            return false;
        }
        self::log("method: " . __FUNCTION__ . "\tparams: " . $appId . "\tenv: {$env}\treturn: " . json_encode($result), 
        __CLASS__, true);
        return true;
    }
    /**
     * 自检函数
     */
    public static function selfTest ()
    {
        $arr = array(
            sdk_fetch_config(SDK_ENV . self::AUTHEN_APP_OP_URL), 
            sdk_fetch_config(SDK_ENV . self::AUTHEN_APP_SEARCH_URL), 
            sdk_fetch_config(SDK_ENV . self::MODIFY_PERM_USER)
        );
        return self::curlTest($arr);
    }
}
